/**
 * Created by user on 17/2/16.
 * koa context extension
 */
let JsonResponse = require('../lib/JsonResponse');

module.exports = function(app) {
    Object.defineProperty(app.context, 'JsonResponse', {
        get: function () {
            if (this._JsonResponse) {
                return this._JsonResponse;
            }

            const jsonResponse = new JsonResponse(this);
            this._JsonResponse = jsonResponse;
            return jsonResponse;
        }
    });

    Object.defineProperty(app.context, 'getRealIp', {
        get: function () {
            let self = this;
            return function () {
                let headers = self.headers;
                console.log('headers: ', headers);
                let realIp = headers['remote-host'];
                if (!headers['x-forwarded-for']) {
                    realIp = headers['x-forwarded-for'];
                } else if (!headers['x-real-ip']) {
                    realIp = headers['x-real-ip'];
                }
                return realIp ? realIp : '0.0.0.0';
            };
        }
    });
};
